package org.example.mybatis_plus;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.mybatisplus.MybatisPlusApplication;
import org.example.mybatisplus.entity.User;
import org.example.mybatisplus.service.IUserService;
import org.example.mybatisplus.service.impl.UserServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest(classes = MybatisPlusApplication.class)
public class MybatisPlusPage {

    @Autowired
    private IUserService userService;

    @Test
    public void testPageQuery() {
        int pageNo = 2;
        int pageSize = 3;
        // 分页参数
        Page<User> page = Page.of(pageNo, pageSize);
        // 排序参数
        page.addOrder(new OrderItem().setColumn("balance").setAsc(false));
        // 分页查询
        Page<User> userPage = userService.page(page);
        // 总条数
        System.out.println("total="+userPage.getTotal());
        // 总页数
        System.out.println("pages="+userPage.getPages());
        // 分页数据
        List<User> records = userPage.getRecords();
        System.out.println("records="+records.size());
        records.forEach(System.out::println);
    }

}
